﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Phone.UI.Input;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// “用户控件”项模板在 http://go.microsoft.com/fwlink/?LinkId=234236 上提供

namespace Itcast.Chat.Controls
{
    public sealed partial class ExitTip : UserControl
    {
        public ExitTip()
        {
            this.InitializeComponent();
            HardwareButtons.BackPressed += HardwareButtons_BackPressed;
        }

        bool isExiting = false;
        void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
        {
            #region 异步实现
            //e.Handled = true;
            //// 短时间内（2秒钟）点两次后退键  则退出应用程序
            //if (isExiting)
            //{
            //    // 退出应用程序
            //    App.Current.Exit();
            //}
            //else
            //{
            //    // 展示正在退出提示 并将isExiting设为true
            //    exitTips.Visibility = Windows.UI.Xaml.Visibility.Visible;
            //    isExiting = true;
            //    //await Task.Run(() => { 

            //    //});
            //    await Task.Delay(2000);
            //    exitTips.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            //    isExiting = false;
            //} 
            #endregion

            if (isExiting)
            {
                App.Current.Exit();
            }
            else
            {
                exitTipShow.Begin();
                isExiting = true;
                exitTipShow.Completed += exitTipShow_Completed;
            }
            e.Handled = true;
        }

        void exitTipShow_Completed(object sender, object e)
        {
            // 动画播放完成 结束退出状态
            isExiting = false;
            exitTipShow.Completed -= exitTipShow_Completed;
        }
    }
}
